/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.reader.data.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownHttp {
	URLConnection conn;
	private URL downUrl;
	private HttpURLConnection http;
	private String mPath;

	public DownHttp(String paramString) {
		this.mPath = paramString;
		try {
			this.downUrl = new URL(paramString);
			return;
		} catch (MalformedURLException localMalformedURLException) {
			localMalformedURLException.printStackTrace();
		}
	}

	private String getFileName(HttpURLConnection paramHttpURLConnection,
			String paramString) {
		String str1 = paramString.substring(1 + paramString.lastIndexOf(47));
		if ((str1 == null) || ("".equals(str1.trim())))
			;
		for (int i = 0;; ++i) {
			String str2 = paramHttpURLConnection.getHeaderField(i);
			if (str2 == null) {
				str1 = UUID.randomUUID() + ".tmp";
				return str1;
			}
			if (!("content-disposition".equals(paramHttpURLConnection
					.getHeaderFieldKey(i).toLowerCase())))
				continue;
			Matcher localMatcher = Pattern.compile(".*filename=(.*)").matcher(
					str2.toLowerCase());
			if (localMatcher.find())
				return localMatcher.group(1);
		}
	}

	public int getFileLength() {
		return this.http.getContentLength();
	}

	public String getFileName() {
		return getFileName(this.http, this.mPath);
	}

	public InputStream getInputStream() throws IOException {
		this.http = ((HttpURLConnection) this.downUrl.openConnection());
		this.http.setConnectTimeout(5000);
		this.http.setRequestMethod("GET");
		this.http
				.setRequestProperty(
						"Accept",
						"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		this.http.setRequestProperty("Accept-Language", "zh-CN");
		this.http.setRequestProperty("Referer", this.downUrl.toString());
		this.http.setRequestProperty("Charset", "UTF-8");
		this.http
				.setRequestProperty(
						"User-Agent",
						"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
		this.http.setRequestProperty("Connection", "Keep-Alive");
		return this.http.getInputStream();
	}
}